;(function() {
		init_parsley();
		init_data();
		/***
		 * 初始化数据
		 */
		function init_data(){
			if($("#is_bind_face").attr("data-key")==1){
				$("#is_bind_face").show();
			}else{
				$("#is_bind_face").hide();
			}
			
			if($("#is_bind_finger").attr("data-key")==1){
			 	$("#is_bind_finger").show();
			}else{
			 	$("#is_bind_finger").hide();
			}
		}
		
		$(".btn_fingerprint").click(function(){
		    var step=$(this).attr("data-step");
		    var csharp = window.external;
		    if(step=="1"){
		    	csharp.SignFinger1();	
		    }else if(step==2){
		    	csharp.SignFinger2();	
		    }else{
		    	alert("无效的点击");
		    }
		});

		$("#in_varifycation_type").change(function(){
			if($(this).val()!=0){
				$('#in_varifycation').val(1);
			}else{
				$('#in_varifycation').val(0);
			}
		})
		
		/**修改人脸*/
		$(".face_update").click(function(){
			open_camare("2");
			return false;
		})
		/**结束修改*/
		$(".face_update_over").click(function(){
			var card_no=$("#card_no").val();
			if(card_no.length<1){
				toastr['error']("卡号不正确","操作失败！"); 
				return false;
			}
			
			var data_param=$(this).attr("data-param");
			var data_param_index=parseInt(data_param)+1;
			
			var csharp=window.external;
			var face_edit= csharp.GetEditFaceCode(card_no);
			if(face_edit==null||face_edit.length<1000){
				toastr["error"]("模板不清晰，请重新拍摄");
				return false;
			}
			var icon_edit=csharp.GetFaceUrl();
			$("#face_image"+data_param_index).attr("src",icon_edit);
			
			var icons=$("#icon1").val().split(",");
			var faces=$("#face1").val().split(",");
			for (var i = 0; i < 3; i++) {//补全3个
				if(icons[i]=='undefined'||icons[i]==null||icons[i].length<10){
					icons[data_param]="";
					faces[data_param]="";	
				}
			}
			
			var icons_copy=[];
			var faces_copy=[];
			if(icons.length>=(data_param)){
				icons[data_param]=icon_edit;
				faces[data_param]=face_edit;	
			}
			else{
					return false;
			}
			for (var i=0;i<icons.length;i++) { //清理空的数组
				if(icons[i]!='undefined'&&icons[i]!=null&&icons[i].length>10){
					icons_copy.push(icons[i]);
					faces_copy.push(faces[i]);
				}
			}
			$("#icon1").val(icons_copy.join(","));
			$("#face1").val(faces_copy.join(","));
		});
		/**删除模板*/
		$(".face_delete").click(function(){
				var data_param=$(this).attr("data-param");
				var data_param_index=parseInt(data_param)+1;
				$("#face_image"+data_param_index).attr("src","/data/finger/a8.jpg");
				var icons=$("#icon1").val().split(",");
				var faces=$("#face1").val().split(",");
				var icons_copy=[];
				var faces_copy=[];
				if(icons.length>=(data_param)){
					icons[data_param]="";
					faces[data_param]="";	
				}
				else{
						return false;
				}
				for (var i=0;i<icons.length;i++) {
					if(icons[i].length>10){
						icons_copy.push(icons[i]);
						faces_copy.push(faces[i]);
					}
				}
				$("#icon1").val(icons_copy.join(","));
				$("#face1").val(faces_copy.join(","));
				$("#form-submit").trigger("click");
				return true;
		});
		
		$(".btn_face_camare").click(function(){
			
			var step=$(this).attr("data-step");
			if(step=="1"){
				open_camare(0);
			}else if(step=="2"){
				var card_no=$("#card_no").val();
				if(card_no.length<1){
					toastr['error']("卡号不正确","操作失败！"); 
					return false;
				}
				var csharp=window.external;
				var faces= csharp.GetFaceCode(card_no);
				if(faces==null||faces.length<1000){
					toastr["error"]("模板不清晰，请重新拍摄");
					return false;
				}
				faces=faces.split(",");
				var arr=csharp.GetFaceUrl().split(",");
				if(arr.length==1){
					$("#face_image1").attr("src",arr[0]);
					$("#face_image2").attr("src","/data/finger/a8.jpg");
					$("#face_image3").attr("src","/data/finger/a8.jpg");
					$("#icon1").val(arr[0]);	
					$("#face1").val(faces[0]);
				}
				if(arr.length==2){
					$("#face_image1").attr("src",arr[0]);
					$("#face_image2").attr("src",arr[1]);
					$("#face_image3").attr("src","/data/finger/a8.jpg");
					$("#icon1").val(arr[0]+","+arr[1]);
					$("#face1").val(faces[0]+","+faces[1]);
				}
				if(arr.length==3){
					$("#face_image1").attr("src",arr[0]);
					$("#face_image2").attr("src",arr[1]);
					$("#face_image3").attr("src",arr[2]);
					$("#icon1").val(arr[0]+","+arr[1]+","+arr[2]);
					$("#face1").val(faces[0]+","+faces[1]+","+faces[2]);
				}
			}else{
				alert("无效的点击");
			}
		   return false;											
		
		})
		
	   	$("#form-submit").click(function(){
	   		var flag=$('#validate-form').parsley('validate' );
	   		if(flag){
	   			if($("#in_varifycation").val()==1){//需要验证
		   			var varify_type=$("#in_varifycation_type").val();
		   			if(varify_type==1){// 指纹验证
		   				if($("#fingerprint").val().length<1000){
		   					toastr['error']("请录入指纹模板");
		   					return false;
		   				}
		   			}else if(varify_type==2){// 人脸验证
		   				if($("#face1").val().length<1000){
		   					toastr['error']("请录入人脸模板");
		   					return false;
		   				}
		   			}
	   			}
	   			loading();
	   			$.post(
	   				"/admin/vice/edit",
	   				$("#validate-form").serialize(),
	   				function(data){
	   					if($("#in_varifycation").val()==1){//需要验证
	   							var varify_type=$("#in_varifycation_type").val();
					   			if(varify_type==1){// 指纹验证
					   				write_txt_finger($("#card_no").val(),$("#fingerprint").val());
					   			}else if(varify_type==2){// 人脸验证
					   				write_txt_face($("#card_no").val(),$("#face1").val());
					   			}
	   					}
	   					close_loading();
			      		if(data.flag){
			      			$("#myModal").modal("hide");
			      			toastr['success']("操作成功！", ''); 
			      			$("#myform #btn-advanced-refresh").trigger("click");// 刷新页面
			      		}else{
			      		 	toastr['error'](data.msg,"操作失败！"); 
			      		}
	   				}
	   			);
	   		}
	   	})
	   	
})();
